package com.ruoyi.shop.cms.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.shop.cms.mapper.ShopCmsArticleMapper;
import com.ruoyi.shop.cms.domain.ShopCmsArticle;
import com.ruoyi.shop.cms.service.IShopCmsArticleService;
import com.ruoyi.shop.common.constant.ShopConstants;
import com.ruoyi.shop.common.utils.ShopUtils;
import com.ruoyi.common.core.text.Convert;

/**
 * 文章Service业务层处理
 * 
 * @author xuxiang
 * @date 2023-06-21
 */
@Service
public class ShopCmsArticleServiceImpl implements IShopCmsArticleService {
	@Autowired
	private ShopCmsArticleMapper shopCmsArticleMapper;

	/**
	 * 查询文章
	 * 
	 * @param articleId 文章主键
	 * @return 文章
	 */
	@Override
	public ShopCmsArticle selectArticleById(Long articleId) {
		ShopCmsArticle article = shopCmsArticleMapper.selectArticleById(articleId);
		// 处理图片路径
		String content = article.getContent().replaceAll(ShopConstants.SHOP_DOMAIN_PREFIX, ShopUtils.getDomain());
		article.setContent(content);
		return article;
	}

	/**
	 * 查询文章列表
	 * 
	 * @param shopCmsArticle 文章
	 * @return 文章
	 */
	@Override
	public List<ShopCmsArticle> selectArticleList(ShopCmsArticle shopCmsArticle) {
		return shopCmsArticleMapper.selectArticleList(shopCmsArticle);
	}

	/**
	 * 新增文章
	 * 
	 * @param shopCmsArticle 文章
	 * @return 结果
	 */
	@Override
	public int insertArticle(ShopCmsArticle shopCmsArticle) {
		shopCmsArticle.setCreateTime(DateUtils.getNowDate());
		// 处理图片路径
		String content = shopCmsArticle.getContent().replaceAll(ShopUtils.getDomain(),ShopConstants.SHOP_DOMAIN_PREFIX);
		shopCmsArticle.setContent(content);
		return shopCmsArticleMapper.insertArticle(shopCmsArticle);
	}

	/**
	 * 修改文章
	 * 
	 * @param shopCmsArticle 文章
	 * @return 结果
	 */
	@Override
	public int updateArticle(ShopCmsArticle shopCmsArticle) {
		shopCmsArticle.setUpdateTime(DateUtils.getNowDate());
		// 处理图片路径
		String content = shopCmsArticle.getContent().replaceAll(ShopUtils.getDomain(),ShopConstants.SHOP_DOMAIN_PREFIX);
		shopCmsArticle.setContent(content);
		return shopCmsArticleMapper.updateArticle(shopCmsArticle);
	}

	/**
	 * 批量删除文章
	 * 
	 * @param articleIds 需要删除的文章主键
	 * @return 结果
	 */
	@Override
	public int deleteArticleByIds(String articleIds) {
		return shopCmsArticleMapper.deleteArticleByIds(Convert.toStrArray(articleIds));
	}

	/**
	 * 删除文章信息
	 * 
	 * @param articleId 文章主键
	 * @return 结果
	 */
	@Override
	public int deleteArticleById(Long articleId) {
		return shopCmsArticleMapper.deleteArticleById(articleId);
	}

	/**
	 * 查询文章列表
	 * 
	 * @param shopCmsArticle
	 * @return
	 */
	@Override
	public List<ShopCmsArticle> queryArticles(ShopCmsArticle shopCmsArticle) {
		shopCmsArticle.setStatus(ShopConstants.YES);
		List<ShopCmsArticle> list = shopCmsArticleMapper.selectArticleList(shopCmsArticle);
		list.forEach(article -> article.setImage(ShopUtils.getAbsoluteUrl(article.getImage())));
		return list;
	}

	/**
	 * 查询轮播图
	 * 
	 * @param shopCmsArticle
	 * @return
	 */
	@Override
	public List<ShopCmsArticle> querySwipers() {
		ShopCmsArticle shopCmsArticle = new ShopCmsArticle();
		shopCmsArticle.setStatus(ShopConstants.YES);
		shopCmsArticle.setIsShow(ShopConstants.YES);
		List<ShopCmsArticle> list = shopCmsArticleMapper.selectArticleList(shopCmsArticle);
		list.forEach(article -> article.setImage(ShopUtils.getAbsoluteUrl(article.getImage())));
		return list;
	}

	/**
	 * 查询文章排行榜
	 * 
	 * @return
	 */
	@Override
	public List<ShopCmsArticle> queryTopList() {
		return shopCmsArticleMapper.queryTopList();
	}

}
